package cn.wolfcode.trip.admin.controller;

import cn.wolfcode.trip.base.domain.Strategy;
import cn.wolfcode.trip.base.domain.TravelCommend;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyQueryObject;
import cn.wolfcode.trip.base.service.IRegionService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

/**
 * 大攻略资源控制器
 *
 * @author totoro93
 * @date 2019-01-11
 **/
@RequestMapping("/strategy")
@Controller
public class StrategyController {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IRegionService regionService;

    /**
     * 大攻略管理列表
     *
     * @param queryObject
     * @param model
     * @return
     */
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") StrategyQueryObject queryObject, Model model) {
        model.addAttribute("regions", regionService.listAll(null));
        model.addAttribute("pageInfo", strategyService.query(queryObject));
        return "strategy/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JSONResult saveOrUpdate(Strategy strategy, MultipartFile file) {
        if (file != null && file.getSize() > 0) {
            //String url = UploadUtil.upload(file, UploadUtil.PATH + "/upload/");
            String url = UploadUtil.uploadQiniu(file);
            strategy.setCoverUrl(url);
        }
        strategyService.saveOrUpdate(strategy);
        return new JSONResult();
    }


}
